//
//  HomeTableViewController.swift
//  01-xiangmu
//
//  Created by Apple on 15/11/4.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit
import SVProgressHUD
///可重用cell
 let StatusCellNormalId = "StatusCellNormalId"
/// 转发微博的可重用 ID
let StatusCellRetweetedId = "StatusCellRetweetedId"
class HomeTableViewController:VisitorTableViewController {
    private lazy var ListViewModel = StatusListViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if !UserAccountViewModel.shardViewModel.userLogin {
            visitorView?.setupInfo(nil, title: "关注一些人，回这里看看有什么惊喜")
            return
        }
        prepareTableView()
        loadData()
    }
    /// 准备表格
    private func prepareTableView() {
        //隐藏分割条
        tableView.separatorStyle = .None
        tableView.registerClass(StatusNormalCell.self, forCellReuseIdentifier: StatusCellNormalId)
        tableView.registerClass(StatusRetweetedCell.self, forCellReuseIdentifier: StatusCellRetweetedId)
           tableView.estimatedRowHeight = 400
       //下拉刷新 控件默认没有
        refreshControl = WBRefreshControl()
        //添加监听方法
        refreshControl?.addTarget(self, action: "loadData", forControlEvents: UIControlEvents.ValueChanged)
       // 上拉刷新视图
        tableView.tableFooterView = pullupView
    }
    //MARK: - 加载数据
    
   @objc private func loadData () {
    refreshControl?.beginRefreshing()
        ListViewModel.loadStatus(isPullup: pullupView.isAnimating()) { (isSuccessed) -> () in
            //关闭监听方法
            self.refreshControl?.endRefreshing()
            // 关闭上拉刷新
            self.pullupView.stopAnimating()
            if !isSuccessed {
                SVProgressHUD.showInfoWithStatus("加载数据错误，请稍后再试")
                return
            }
            print(self.ListViewModel.statusList.count)
            //刷新数据
            self.tableView.reloadData()
        }
    }
    // MARK: - 懒加载控件
    /// 上拉刷新提示视图
    private lazy var pullupView: UIActivityIndicatorView = {
        
        let indicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
        indicator.color = UIColor.lightGrayColor()
        
        return indicator
    }()

}
//MARK: - 数据源方法
extension HomeTableViewController {
    //行
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return ListViewModel.statusList.count
    }
    //行内容
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let vm = ListViewModel.statusList[indexPath.row]
        let cell = tableView.dequeueReusableCellWithIdentifier(vm.cellId, forIndexPath: indexPath) as! StatusCell
        cell.viewModel = vm
        // 4. 判断是否是最后一条微博
        if indexPath.row == ListViewModel.statusList.count - 1 && !pullupView.isAnimating() {
            // 开始动画
            pullupView.startAnimating()
            
            // 上拉刷新数据
            loadData()
            
            print("上拉刷新数据")
        }

        return cell
    }
    // 苹果官方文档有指出，如果行高是固定值，就不要实现行高代理方法！
    
    // 实际开发中，行高一定要缓存！
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return ListViewModel.statusList[indexPath.row].rowHeight
    }
}
